import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Welcome from '../components/Welcome.vue'
import ConferenceRoom from '@/components/conference/ConferenceRoom'
import car from '@/components/conference/car'
import maintain from '@/components/conference/maintain'
import MailService from '@/components/mail/MailService'
import card from '@/components/card/card'
import supper from '@/components/suppers/supper'
import log from '@/components/log/log'
import Furniture from '@/components/furniture/Furniture'

// 安装 Vue.js 插件。如果插件是一个对象，必须提供 install 方法。如果插件是一个函数，它会被作为 install 方法。install 方法调用时，会将 Vue 作为参数传入
Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/welcome',
    name: 'welcome',
    component: Welcome,
    // 一级路由时默认自动跳转指定的二级路由页面
    // redirect: '/welcome/emp',
    children: [
      {
        path: 'conferenceRoom',
        component: ConferenceRoom
      }, {
        path: 'MailService',
        component: MailService,
      },
      {
        path: 'car',
        component: car
      },
      {
        path: 'card',
        component: card
      },
      {
        path: 'supper',
        component: supper
      },
      {
        path: 'maintain',
        component: maintain
      }
      ,
      {
        path: 'log',
        component: log
      },
      {
        path: 'furniture',
        component: Furniture
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  }
]

const router = new VueRouter({
  routes,
  mode: 'hash' // hash:默认’hash’值，但是hash看起来就像无意义的字符排列，不太好看也不符合我们一般的网址浏览习惯。
  // mode:"history"   //hash url 跳转histroy:当你使用 history 模式时，URL 就像正常的 url，例如 http://localhost:8080/hi，也好看！
})

// 路由守卫应该写到const router 后面
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const store = window.sessionStorage.getItem('storeData')
  console.log(store)
  if (store == null) {
    return next('/login')
  }
  next()
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
